layui.use(['projectprofile', 'laytpl', "element", "jquery"], function() {
	var laytpl = layui.laytpl,
		element = layui.element,
		$ = layui.jquery;
	var costId = wang.getQueryString("costId");
	var individualSectectList = [];
	var getBaseInfo = function(costId) { //获取项目基本信息
		$.ajax({
			type: "post",
			url: "/guidance_service/front/data/get.do",
			data: {
				costId: costId
			},
			success: function(result) {
				var data = result.data; // first
				$("#costName").text(data.costName);
				var top = top1.innerHTML; //	顶部详细信息
				laytpl(top).render(data, function(html) {
					$("#topHei").html(html);
				});
				if(data.survey) { //	工程概况
					var survey = JSON.parse(data.survey);
					var shareType = wang.getQueryString("catalogId");
					var gcgkMode = gcgk1.innerHTML;
					laytpl(gcgkMode).render(survey, function(html) {
						$("#gcgkBox").html(html);
					});
				}
				var rightLift1 = rightLift.innerHTML; // 右侧楼层电梯
				laytpl(rightLift1).render(rightLift1, function(html) {
					$("#rightLiftBox").html(html);
				});
				$(".recommendText").on("mouseover", function() {
					$(this).siblings('.recommendMessage').show();
					$(this).hide();
				});
				$(".recommendMessage").on("mouseout", function() { //以下两行加入的封闭括号
					$(this).siblings('.recommendText').show();
					$(this).hide();
				})
			}
		});
	}
	getBaseInfo(costId);
	var getpieOption = function(data) {
		var pieOption = {
			chart: {
				type: 'pie',
				options3d: {
					enabled: true,
					alpha: 45,
					beta: 0
				}
			},
			colors: ['#7cb5ec', '#434348', '#90ed7d', '#f7a35c', '#8085e9',
				'#f15c80', '#e4d354', '#8085e8', '#8d4653', '#91e8e1'
			],
			title: {
				text: '指标分析图'
			},
			tooltip: {
				pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
			},
			plotOptions: {
				pie: {
					allowPointSelect: true,
					cursor: 'pointer',
					depth: 35,
					dataLabels: {
						enabled: true,
						format: '{point.name}'
					}
				}
			},
			series: [{
				type: 'pie',
				name: '指标占比',
				data: data
			}]
		};
		return pieOption;
	}
	var getColumnOption = function(slenderCategories, slenderData) {
		var option = {
			chart: {
				type: 'column',
				options3d: {
					enabled: true,
					alpha: 0,
					beta: 15,
					viewDistance: 25,
					depth: 40
				},
				marginTop: 80,
				marginRight: 40
			},
			title: {
				text: '造价组成分析图'
			},
			xAxis: {
				categories: slenderCategories
			},
			yAxis: {
				allowDecimals: false,
				min: 0,
				title: {
					text: ''
				}
			},
			tooltip: {
				headerFormat: '<b>{point.key}</b><br>',
				pointFormat: '<span style="color:{series.color}">\u25CF</span> {series.name}: {point.stackTotal}'
			},
			plotOptions: {
				column: {
					stacking: 'normal',
					depth: 40
				}
			},
			series: slenderData
		}
		return option;
	}
	var getMajorInfo = function() {
		$.ajax({
			type: "post",
			url: "/guidance_service/front/data/getAnalyze.do",
			data: {
				costId: costId
			},
			success: function(result) {
				if(result.data.collect) {
					var list = result.data.collect || [];
					var individualNameHtml = "";
					$("#collectSelect").show().html('<option value="_">请选择单项工程</option>');
					for(var l in list) {
						if(l == '0') {
							individualNameHtml += '<li class="layui-this">' + list[l].individualName + '</li>';
							$("#collectSelect").append('<option selected="selected"  value="' + list[l].collectId + '">' + list[l].individualName + '</option>');
						} else {
							individualNameHtml += '<li>' + list[l].individualName + '</li>';
							$("#collectSelect").append('<option value="' + list[l].collectId + '">' + list[l].individualName + '</option>');
						}
						var individual = list[l].individual;
						individualSectectList = individualSectectList.concat(individual);
						laytpl(zjzcfxTabItem.innerHTML).render(individual, function(html) {
							$("#zjzcfxTabItemHtml").append(html);
						});
						//此处改动，将laytpl的逻辑内容拿出来放入新的循环中，以消除，对循环加载图表的影响
						$.each(individual, function(i, v) {
							var roundData = [];
							var slenderCategories = [];
							var slenderData = [{
									name: '分部分项工程费',
									data: [],
									stack: 'first'
								},
								{
									name: '措施项目费',
									data: [],
									stack: 'second'
								},
								{
									name: '其他项目费',
									data: [],
									stack: 'third'
								},
								{
									name: '规费税金',
									data: [],
									stack: 'fourth'
								}
							];
							for(var d in individual) {
								roundData.push([individual[d].unitName, Number(individual[d].costTotal)]);
								slenderCategories.push(individual[d].unitName);
								slenderData[0].data.push(Number(individual[d].infoTotal));
								slenderData[1].data.push(Number(individual[d].measuresTotal));
								slenderData[2].data.push(Number(individual[d].otherTotal));
								slenderData[3].data.push(Number(individual[d].taxesTotal));
							}

							$('.item-container-column').eq(l).highcharts(getColumnOption(slenderCategories, slenderData));
							$('.item-container-pie').eq(l).highcharts(getpieOption(eval(roundData)));
							$($(".nav-list").eq(l).find("li")[0]).click();
						})

					}
					$("#zjzcfxTab").html(individualNameHtml);
					$(".layui-tab-item").eq(0).addClass("layui-show");
				} else if(result.data.individual) {
					var individual = result.data.individual || [];
					$(".Allsurvey").find(".SingleProjectName").remove();
					$(".Allsurvey").find("#zjzcfxTab").remove();
					individualSectectList = individualSectectList.concat(individual);
					laytpl(zjzcfxTabItem.innerHTML).render(individual, function(html) {
						$("#zjzcfxTabItemHtml").append(html);

					});
					//此处改动，将laytpl的逻辑内容拿出来放入新的循环中，以消除，对循环加载图表的影响
					$.each(individual, function(i, v) {
						var roundData = [];
						var slenderCategories = [];
						var slenderData = [{
								name: '分部分项工程费',
								data: [],
								stack: 'first'
							},
							{
								name: '措施项目费',
								data: [],
								stack: 'second'
							},
							{
								name: '其他项目费',
								data: [],
								stack: 'third'
							},
							{
								name: '规费税金',
								data: [],
								stack: 'fourth'
							}
						];
						var html = '<option value="_">请选择清单专业</option>';
						for(var d in individual) {
							roundData.push([individual[d].unitName, Number(individual[d].costTotal)]);
							slenderCategories.push(individual[d].unitName);
							slenderData[0].data.push(Number(individual[d].infoTotal));
							slenderData[1].data.push(Number(individual[d].measuresTotal));
							slenderData[2].data.push(Number(individual[d].otherTotal));
							slenderData[3].data.push(Number(individual[d].taxesTotal));
							var value = individual[d].individualId + "_" + individual[d].majorId;
							if(d == 0) {
								html += '<option selected="selected"  value="' + value + '">' + individual[d].unitName + '</option>';
							} else {
								html += '<option   value="' + value + '">' + individual[d].unitName + '</option>';
							}

						}
						$("#individualSelect").show().html(html);

						$('.item-container-pie').eq(i).highcharts(getpieOption(roundData));
						$('.item-container-column').eq(i).highcharts(getColumnOption(slenderCategories, slenderData));
						$($(".nav-list").find("li")[0]).click();
					})
					$(".layui-tab-item").eq(0).addClass("layui-show");
				} else if(result.data.major) {
					var major = result.data.major || [];
					$(".Allsurvey").find(".SingleProjectName").remove();
					$(".Allsurvey").find("#zjzcfxTab").remove();
					individualSectectList = individualSectectList.concat(major);
					laytpl(zjzcfxTabItem.innerHTML).render(major, function(html) {
						$("#zjzcfxTabItemHtml").append(html);

					});
					//此处改动，将laytpl的逻辑内容拿出来放入新的循环中，以消除，对循环加载图表的影响
					$.each(major, function(i, v) {

						var roundData = [];
						var slenderCategories = [];
						var slenderData = [{
								name: '分部分项工程费',
								data: [],
								stack: 'first'
							},
							{
								name: '措施项目费',
								data: [],
								stack: 'second'
							},
							{
								name: '其他项目费',
								data: [],
								stack: 'third'
							},
							{
								name: '规费税金',
								data: [],
								stack: 'fourth'
							}
						];
						var html = '<option value="_">请选择清单专业</option>';
						for(var d in major) {
							roundData.push([major[d].name, (Number(major[d].infoTotal) + Number(major[d].measuresTotal) + Number(major[d].otherTotal) + Number(major[d].taxesTotal))]);
							slenderCategories.push(major[d].name);
							slenderData[0].data.push(Number(major[d].infoTotal));
							slenderData[1].data.push(Number(major[d].measuresTotal));
							slenderData[2].data.push(Number(major[d].otherTotal));
							slenderData[3].data.push(Number(major[d].taxesTotal));
							var value = major[d].individualId + "_" + major[d].majorId;
							if(d == 0) {
								html += '<option selected="selected" value="' + value + '">' + major[d].name + '</option>';
							} else {
								html += '<option   value="' + value + '">' + major[d].name + '</option>';
							}
						}
						$("#individualSelect").show().html(html);
						$('.item-container-pie').eq(0).highcharts(getpieOption(roundData));
						$('.item-container-column').eq(0).highcharts(getColumnOption(slenderCategories, slenderData));
						$($(".nav-list").eq(0).find("li")[0]).click();
					})
					$(".layui-tab-item").eq(0).addClass("layui-show");
				}
			}
		});
	}
	getMajorInfo();
	$("#collectSelect").change(function(event) {
		var collectId = $(this).val();
		if(collectId == "_" | collectId == "" | collectId == null) return;
		var html = '<option value="_">请选择清单专业</option>';
		for(var i in individualSectectList) {
			if(individualSectectList[i].collectId == collectId) {
				var value = individualSectectList[i].individualId + "_" + individualSectectList[i].majorId;
				html += '<option   value="' + value + '">' + individualSectectList[i].unitName + '</option>';
			}
		}
		$("#individualSelect").show().html(html);
	});
	$("#individualSelect").change(function(event) {
		var individualId_majorId = $(this).val();
		if(individualId_majorId == "_" | individualId_majorId == "" | individualId_majorId == null) return;
		var individualId = individualId_majorId.split("_")[0];
		var majorId = individualId_majorId.split("_")[1];
		var html = '<option value="_">请选择表格名称</option>';
	    $.ajax({
			type: "post",
			url: "/guidance_service/front/data/getOwnTables.do",
			data: {
				costId: costId,
				individualId:individualId,
				majorId:majorId
			},
			success: function(result) {
				var data = result.data;
				if(data.collect)html += '<option title="建设项目汇总表" value="collect_' + majorId + '">建设项目汇总表</option>';
				if(data.individual)html += '<option title="单项工程汇总表" value="individual_' + majorId + '">单项工程汇总表</option>';
				if(data.unit)html += '<option title="单位工程汇总表" value="unit_' + majorId + '" >单位工程汇总表</option>';
				if(data.info)html += '<option title="分部分项工程和单价措施项目清单与计价表" value="info_' + majorId + '" >分部分项工程和单价措施项目清单与计价表</option>';
				if(data.measures)html += '<option title="总价措施项目清单与计价表" value="measures_' + majorId + '" >总价措施项目清单与计价表</option>';
				if(data.other)html += '<option title="其他项目清单与计价汇总表" value="other_' + majorId + '" >其他项目清单与计价汇总表</option>';
				if(data.taxes)html += '<option title="规费、税金项目清单与计价表" value="taxes_' + majorId + '" >规费、税金项目清单与计价表</option>';
				if(data.machine)html += '<option title="单位工程人材机汇总表" value="machine_' + majorId + '"  >单位工程人材机汇总表</option>';
				$("#majorSelect").show().html(html);
				$("#majorSelect").change(function(event) {
					$(".loderMore button").remove();
					var costId = wang.getQueryString("costId");
					var collectId = $("#collectSelect").val();
					if(collectId == null || collectId == "") collectId = "0000";
					if($("#individualSelect").val() == "_" | $("#individualSelect").val() == "" | $("#individualSelect").val() == null) return;
					var individualId = $("#individualSelect").val().split("_")[0];
					if(individualId == null || individualId == "") collectId = "1111";
					var value = $(this).val();
					if(value == "_" | value == null) return;
					var type = value.split("_")[0];
					var majorId = value.split("_")[1];
					var data = {}
					data.costId = costId;
					switch(type) {
						case "collect":
							data.costId = costId;
							break;
						case "individual":
							data.collectId = collectId;
							break;
						case "unit":
							data.majorId = majorId;
							break;
						case "info":
							data.majorId = majorId;
							break;
						case "measures":
							data.majorId = majorId;
							break;
						case "other":
							data.majorId = majorId;
							break;
						case "taxes":
							data.majorId = majorId;
							break;
						case "machine":
							data.majorId = majorId;
							break;
					}
					getShareList({type:type,data:data});
				});
			    var getPositioninfo =function(ops){
			    	$.post("/guidance_service/front/data/info/getPositionName.do",{"costId":costId,"majorId":majorId},
						function(rlt){
							var html = '<li data-id='+rlt.data[0].positionCode+' class="active">'+rlt.data[0].positionName+'</li>';
							for(var i=1;i<rlt.data.length;i++){
								html +='<li data-id='+rlt.data[i].positionCode+' >'+rlt.data[i].positionName+'</li>';
							}
							$("#infoPosition").html(html).find("li").click(function(){
								console.dir($(this).data('id'));
								var positionId =$(this).data('id') || "";
								$.post("/guidance_service/front/data/" + type + "/getAll.do",{"costId":costId,"majorId":majorId,positionId:positionId},
									function(result){
										var data = JSON.parse(JSON.stringify(result.data).replace(/\\n/g, "<br>"));
										laytpl(tpl).render(data, function(html) {
											$("#gxqdListBox").html(html);
										});
									})
							});
					});
			    }
				var getShareList =function(ops){
					$.ajax({
						type: "post",
						url: "/guidance_service/front/data/" + ops.type + "/getAll.do",
						data: ops.data,
						success: function(result) {
						    var data = JSON.parse(JSON.stringify(result.data).replace(/\\n/g, "<br>"));
							switch(ops.type) {
									case "collect":
										tpl = collectHTML.innerHTML;
										break;
									case "individual":
										tpl = individualHTML.innerHTML;
										break;
									case "unit":
										tpl = unitHTML.innerHTML;
										break;
									case "info":
										tpl =infoHTML.innerHTML;
										break;
									case "measures":
										tpl = measuresHTML.innerHTML;
										break;
									case "other":
										tpl = otherHTML.innerHTML;
										break;
									case "taxes":
										tpl = taxesHTML.innerHTML;
										break;
									case "machine":
										tpl = machineHTML.innerHTML;
										break;
							}
//							if(data.length > 30) {
//								var firstData=[]
//								for(var i=0;i<30;i++){
//									firstData.push(data[i]);
//								};
//								laytpl(tpl).render(firstData, function(html) {
//									$("#gxqdListBox").html(html);
//									if(type =="info"){
//										$.post("/guidance_service/front/data/info/getPositionName.do",{"costId":costId,"majorId":majorId},
//											function(rlt){
//												var html = '<li data-id='+rlt.data[0].positionId+' class="active">'+rlt.data[0].positionName+'</li>';
//												for(var i=1;i<rlt.data.length;i++){
//													html +='<li data-id='+rlt.data[i].positionId+' >'+rlt.data[i].positionName+'</li>';
//												}
//												$("#infoPosition").html(html).find("li").click(function(){
//													console.dir($(this).data('id'));
//													var positionId =$(this).data('id') || "";
//													$.post("/guidance_service/front/data/" + type + "/getAll.do",{"costId":costId,"majorId":majorId,positionId:positionId},
//														function(result){
//															var data = JSON.parse(JSON.stringify(result.data).replace(/\\n/g, "<br>"));
//															laytpl(tpl).render(data, function(html) {
//																$("#gxqdListBox").html(html);
//															});
//														})
//												});
//											})
//									}
//								});
//								var lodeMoretml='<button type="button" class="layui-btn layui-btn-primary">加载全部</button>'
//								$(".loderMore").html(lodeMoretml);					
//								$(".loderMore button").click(function(){
//									var moreData=[]
//									for(var i=30;i<data.length;i++){
//										moreData.push(data[i]);
//									};
//									var moretpl=infoMoreHTML.innerHTML;
//									laytpl(moretpl).render(moreData, function(html) {
//										$("#gxqdListBox .dwgcrcj").append(html);
//									});
//								$(".loderMore button").remove();
//								})
//							} else {					
								laytpl(tpl).render(data, function(html) {
									$("#gxqdListBox").html(html);
									if(type =="info"){
										getPositioninfo();
									}
								});
//								$("#topSection").hide();
//							}
						}
					});
				}
				$("#majorSelect").find("option").eq('1').attr("selected",true);
				$("#majorSelect").trigger("change");
			}
		});	
	});
});